#!/bin/bash

trap "echo '启动失败！！！'; exit" SIGHUP SIGINT SIGTERM SIGFPE

#镜像名 容器名
IMAGE_NAME=hyy/python:collect
CONTAINER=hyy_collect

#镜像id 容器id
IID=$(docker images -q "$IMAGE_NAME" 2> /dev/null)
CID=$(docker ps | grep "$CONTAINER" | awk '{print $1}')

#更新代码
git pull || exit
echo "更新代码成功"

# 创建镜像
if [ -n "$IID" ]; then
    echo "$IMAGE_NAME 镜像存在，IID=$IID"
else
    echo "$IMAGE_NAME 镜像不存在，开始构建镜像..."
    docker build -t $IMAGE_NAME . || exit
		echo "$IMAGE_NAME 镜像创建成功"
fi

#启动容器
if [ -n "$CID" ]; then
    docker-compose restart || exit
else
    echo "正在创建容器..."
    docker-compose up -d || exit
fi

echo "$CONTAINER 容器启动成功"


